<template>
    <el-container class="award-kind">
        <el-main class="award-kind__content">
            <div ref="container" class="table-container">
                <div class="award-kind__header-main">
                    <el-button size="small" type="primary" @click="openDialog('add')">添加表彰分类</el-button>
                </div>
                <el-table ref="table" :data="datas" style="width: 100%" align="center" header-align="center">
                    <el-table-column label="名称" prop="kindTitle" />
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-switch v-model="scope.row.isLatest" :disabled="scope.row.isLatest"
                                active-text="官网最新展示" :inactive-value="0" :active-value="1"
                                @change="(value) => { setLatest(scope.row, value) }">
                            </el-switch>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </el-main>

        <el-dialog :title="dialogText" :visible.sync="dialogVisible" width="400" center :lock-scroll="true" top="40px"
            :close-on-press-escape="false" :close-on-click-modal="false" v-if="dialogVisible">
            <el-form label-width="160px">
                <el-form-item label="名称" required>
                    <el-input v-model="formData.kindTitle" placeholder="名称" />
                </el-form-item>

            </el-form>
            <span slot="footer">
                <el-button @click="cancel">取消</el-button>
                <el-button type="primary" @click="handleCreate">确定</el-button>
            </span>
        </el-dialog>
    </el-container>
</template>

<script>
export default {
    name: "award-kindList",

    data() {
        return {
            datas: [],
            dialogVisible: false,
            dialogVisibleType: "",
            formData: {
                kindTitle: "",
            },
        };
    },

    computed: {
        dialogText() {
            return "添加";
        },
    },

    created() {
        this.handleDatas();
    },
    methods: {
        async handleDatas() {
            const loading = this.$loading();
            try {
                const { data } = await this.$fetch.post("/api/award-kind/list", {});
                this.datas = data;
            } catch (error) {
                console.log(error);
            }
            loading.close();
        },
        refresh() {
            this.handleDatas();
        },

        openDialog(type, row) {
            this.dialogVisibleType = type;
            this.dialogVisible = true;
            this.formData.kindTitle = "";
        },

        async handleCreate() {
            const params = {
                kindTitle: this.formData.kindTitle,
            };
            const { code } = await this.$fetch.post("/api/award-kind/create", params);
            if (code === 0) {
                this.$message.success("成功");
                this.cancel()
            }

        },

        handleDelete(row) {
            const params = {
                id: row.id,
            };
            this.$confirm("确定要删除？", "", {
                distinguishCancelAndClose: true,
                confirmButtonText: "确认",
                cancelButtonText: "取消",
            }).then(async () => {
                const { code } = await this.$fetch.post("/api/award-kind/delete", params);
                if (code === 0) {
                    this.$message.success("删除成功");
                }
                this.refresh();
            });
        },
        cancel() {
            this.dialogVisible = false;
            this.refresh();
        },

        async setLatest(row) {

            const params = {
                id: row.kindId,
            };

            const { code } = await this.$fetch.post("/api/award-kind/setLatest", params);
            if (code === 0) {
                this.$message.success("成功");
                this.refresh();
            }


        }
    },


};
</script>

<style scoped lang="scss">
.award-kind {
    display: flex;
    flex-direction: column;
    height: calc(100vh - 50px);

    .award-kind__header-main {
        height: 40px;
        display: flex;
        align-items: center;
        margin-bottom: 20px;
    }

    .award-kind__content {
        display: flex;
        flex-direction: column;
        flex: 1;
        overflow: auto;
        padding-bottom: 0;

        .table-container {
            flex: 1;
            overflow: hidden;
            overflow-y: auto;
        }
    }

    .pagination-container {
        display: flex;
        justify-content: center;
        padding: 20px 0;

        .el-pagination {
            padding: 20px 0;
        }
    }
}

::v-deep .preview-image {
    width: 360px;
}
</style>
